package com.lshj.impl;

import cn.hutool.core.codec.Base32;
import cn.hutool.core.codec.Base64;
import com.hj.lib.entity.AjaxJson;
import com.hj.lib.impl.HjTioHandle;
import com.lshj.LshjMain;
import javafx.application.Platform;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

import static com.hj.lib.tio.HjClientAioHandler.isServer;
public class HjTioImpl  implements HjTioHandle {
	public void onMessage(AjaxJson ajaxJson) {
		if ("isServer".equals(ajaxJson.getMsg())) {
			isServer = true;
		}
		if (ajaxJson.getCode() == AjaxJson.HjTioCode.TZRH) {
			Platform.runLater(new Runnable() {
				public void run() {
					LshjMain.primaryStage.setMaximized(true);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					LshjMain.primaryStage.setIconified(true);
				}
			});
			String str = ajaxJson.getObj().toString();
			String decodeStr = Base64.decodeStr(str);
			String decodeStrs[] = decodeStr.split("&");
			for (String s : decodeStrs) {
				if (s.indexOf("token=") != -1) {

//					new Thread(new Runnable() {
//						@Override
//						public void run() {
//							try {
//								Thread.sleep(1000*5);
//								Robot robot = new Robot();
//								robot.keyPress(KeyEvent.VK_F12);
//								Thread.sleep(1000);
//								robot.keyRelease(KeyEvent.VK_F12);
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//						}
//					}).start();
					break;
				}
			}
		} else if (ajaxJson.getCode() == AjaxJson.HjTioCode.TCHY) {
			Platform.runLater(new Runnable() {
				public void run() {
					LshjMain.primaryStage.setIconified(true);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					LshjMain.primaryStage.setMaximized(true);
					try {
						Runtime.getRuntime().exec("tskill tc");
					} catch (IOException e) {
						e.printStackTrace();
					}

				}
			});
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//					try {
//						Thread.sleep(1000*5);
//						Robot robot = new Robot();
//						robot.keyPress(KeyEvent.VK_F12);
//						Thread.sleep(1000);
//						robot.keyRelease(KeyEvent.VK_F12);
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}).start();
		}
	}
}
